package com.hz.filemanager.ui.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.lang.ref.WeakReference;

public abstract class BaseFragment extends Fragment {

    protected final String TAG = getClass().getSimpleName();
    
    protected View rootView;
    protected WeakReference<BaseActivity> activity;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = new WeakReference<BaseActivity>((BaseActivity) context);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView == null){
            rootView = inflater.inflate(getLayoutId(),container,false);
            initView(rootView);
            initEvent();
        }
        return rootView;
    }

    protected abstract int getLayoutId();

    protected abstract void initView(View rootView);

    protected abstract void initEvent();

    protected void showToast(String msg){
        activity.get().showToast(msg);
//        Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
    }

    protected void migrateTo(Class clazz){
        activity.get().migrateTo(clazz);
//        getActivity().startActivity(new Intent(getActivity(),clazz));
    }


}
